package com.ruoyi.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.*;
import java.util.Random;

@Configuration
public class GenerateHtmlUtils {



    private static String range = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static String filePath="C:\\Users\\Administrator\\Desktop\\导出数据";

    /**
     * 访问路径
     */
    private static String accessPath="C:\\Users\\Administrator\\Desktop\\导出数据";

    /**
     * 随机生成7位字符串
     *
     * @return
     */
    public static synchronized String getRandomString() {
        Random random = new Random();
        StringBuffer result = new StringBuffer();
        //要生成几位，就把这里的数字改成几
        for (int i = 0; i < 7; i++) {
            result.append(range.charAt(random.nextInt(range.length())));
        }
        return result.toString();
    }

    public static String genHtml(String fileUrl, String htmlContent) {
        String htmlName = "";
        System.out.println("=====URL:" + fileUrl);
        if (StringUtils.isEmpty(fileUrl)) {
            htmlName = "/" + getRandomString() + ".html";
        } else {
            htmlName = fileUrl.substring(fileUrl.lastIndexOf("/"), fileUrl.length());
        }
        String fileName = filePath + htmlName;
        PrintStream printStream = null;
        try {
            printStream = new PrintStream(new FileOutputStream(fileName));
            System.out.println("内容:" + htmlContent);
            printStream.println(htmlContent);
            File file1 = new File(fileName);
            InputStream redis = new FileInputStream(file1);

            OssUtil.uploadToOss(fileUrl, redis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return accessPath + htmlName;
    }


}